<?php

if (!isset($_POST['id'])) header("Location: http://{$_SERVER['HTTP_HOST']}/dict/wordtest.php");

//This is stage 3. First update shelves in db, then print out errors for review by user
//TODO: add checks for submitted variables (?)

require_once("common.inc.php");
require_once("db.inc.php");

auth();
$dbh = get_db();

initSession();

xmlhead("Woorden overhoren - Submit answers and review");

$languages = getLanguages();

$test_mode = fetchSubmittedVariable("test_mode", "post");
if (!($ids = fetchSubmittedVariable("id", "post"))) die ("No data received. Start at <a href=\"wordtest.php\">.");

?>
<table>
<?php

for ($i = 0; $i < count($ids); $i++) {
	if (isset($ids[$i]) && is_numeric($ids[$i])) {
		$l = fetchSubmittedVariable("l{$ids[$i]}", "post");
		$s = fetchSubmittedVariable("s{$ids[$i]}", "post");
		$h = fetchSubmittedVariable("h{$ids[$i]}", "post");
		
		if ($l === false || $s === false || $h === false || !is_numeric($l) || !is_numeric($s) || !is_numeric($h)) {
			var_dump($l, $s, $h);
			die ("missing or illegal input for $i!");
		}
		
		$curid = $ids[$i];
		
	} else {
		
		$s = fetchSubmittedVariable("s{$ids[$i]}", "post");
		var_dump($_POST);
		die ("illegal input for $i!");
		
	}
	//first guesses that are thus not in the table yet
	//insert onto shelf zero regardless of correctness, which is handled separately
	$query = mysql_query(
		"SELECT *
		FROM wvt_wordguesses wg
		WHERE syntrans_sid = {$ids[$i]}
		AND user_id = {$_SESSION['user_id']}
		AND test_mode = $test_mode
		", $dbh
	) or die (mysql_error());
	
	if (mysql_num_rows($query) == 0) {
		mysql_query(
			"INSERT INTO wvt_wordguesses
			VALUES (
				'{$_SESSION['user_id']}',
				'$curid',
				'$l',
				'$test_mode',
				'0',
				NOW()
			)", $dbh
		) or die (mysql_error());
	}
	
	//update scores
	if ($s == 1) { //correct answer, box++
		mysql_query(
			"UPDATE wvt_wordguesses
			SET shelf = shelf + 1,
			versus_language_id = $l,
			lastguess = NOW()
			WHERE syntrans_sid = $curid
			AND user_id = {$_SESSION['user_id']}
			AND test_mode = $test_mode
			", $dbh
		) or die(mysql_error());
	} else if ($s == -1) { //incorrect answer, put into box 0 and print review
		mysql_query(
			"UPDATE wvt_wordguesses
			SET shelf = 0,
			versus_language_id = $l,
			lastguess = NOW()
			WHERE syntrans_sid = $curid
			AND user_id = {$_SESSION['user_id']}
			AND test_mode = $test_mode
			", $dbh
		) or die(mysql_error());
		
		$query = mysql_query(
			"SELECT spelling
			FROM uw_expression e, uw_syntrans s
			WHERE e.expression_id = s.expression_id
			AND s.remove_transaction_id IS NULL
			AND e.remove_transaction_id IS NULL
			AND syntrans_sid = $curid
			", $dbh
		) or die(mysql_error());
		
		if (mysql_num_rows($query) != 1) die ("can't find original expression, syntrans_sid = $curid");
		$foreign_word = mysql_result($query,0,0);
		
		$query = mysql_query(
			"SELECT spelling
			FROM uw_expression e, uw_syntrans s
			WHERE e.expression_id = s.expression_id
			AND s.remove_transaction_id IS NULL
			AND e.remove_transaction_id IS NULL
			AND syntrans_sid = $h
			", $dbh
		) or die(mysql_error());
		
		if (mysql_num_rows($query) != 1) die ("can't find hint expression, syntrans_sid = $h");
		$local_word = mysql_result($query,0,0);
		
		if ($test_mode == 1) {
			?>
<tr>
<td><span class="remember_this"><?php echo $foreign_word;?></span>
<td><span class="remember_this">=&gt;</span>
<td><span class="remember_this"><?php echo $local_word; ?></span>
<?php
		} else if ($test_mode == 2) {
			?>
<tr>
<td><span class="remember_this"><?php echo $local_word; ?></span> (TODO: add dm_text)
<td><span class="remember_this">=&gt;</span>
<td><span class="remember_this"><?php echo $foreign_word;?></span>
<?php
		}
	} else if ($s == 0) { //no action other than touch timestamp
		mysql_query(
			"UPDATE wvt_wordguesses
			SET versus_language_id = $l,
			lastguess = NOW()
			WHERE syntrans_sid = $curid
			AND user_id = {$_SESSION['user_id']}
			AND test_mode = $test_mode
			", $dbh
		) or die(mysql_error());
	}
} // end for $i = [0 .. $_POST[id]]
?>

</table>
<a href="wordtest.php">Back for more</a>
<?php
xmlfoot();
?>